package com.o2opay.car.common.utils;

import com.o2opay.car.common.pojo.RecordRequest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by su on 2017/12/28 08:36
 * 619389279@qq.com
 */
public class TransCode {
//    public static void main(String[] args) {
//        String string = "1B40000000230103000000012017122910102500000000000000000000100083526606D4B1B9A4BCD7B50A";
//        byte[] hex;
//        hex = hexStringToBytes(string);
//        TransRequest(hex);
//    }

    public static RecordRequest TransRequest(byte[] resJson) {
        StringBuffer stringBuffer = new StringBuffer(Util.toHexString(resJson, 0, resJson.length));
        try {
            RecordRequest request = new RecordRequest();
            //byte[] bytes = Util.IntToHex(Integer.valueOf(stringBuffer.substring(0, 3)));
            request.setHead(stringBuffer.substring(0, 4));
            String strLength = stringBuffer.substring(4, 12);
            //int length = isNumeric(strLength) ? Integer.valueOf(strLength) : 0;
            int mLength = Util.HexStrToInt(strLength.getBytes(), strLength.length());
            request.setLength(mLength);
            request.setComman(stringBuffer.substring(12, 16));
            int parmetLength = 16 + (mLength - 2) * 2;
            String mParmet = stringBuffer.substring(16, parmetLength);
            request.setCheckSum(stringBuffer.substring(parmetLength, parmetLength + 2));
            request.setEnd(stringBuffer.substring(parmetLength + 2, parmetLength + 4));
            RecordRequest.Parameter parameter = new RecordRequest.Parameter();
            parameter.setCommand(mParmet.substring(0, 4));
            parameter.setSerialnum(mParmet.substring(0, 8));
            parameter.setSwipeTime(mParmet.substring(8, 22));
            parameter.setCardNo(mParmet.substring(22, 42));
            String employeeNum = mParmet.substring(42, 52);
            String strNameLength = mParmet.substring(52, 54);
            parameter.setEmployee(employeeNum);
            int nameLength = Util.HexStrToInt(strNameLength.getBytes(), strNameLength.length());
            parameter.setEmployLength(nameLength + "");
            byte[] resAscii = new byte[256];
            String hexReadName = mParmet.substring(54, 54 + nameLength * 2);
            Util.HexToAscii(hexReadName.getBytes(), resAscii, hexReadName.length());
            parameter.setReadName(hexReadName);
            request.setParamet(parameter);
            return request;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    private static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

}
